using System;
using System.Windows.Forms;
using System.Text; 
using System.Drawing;
using System.Collections;
using System.IO; 

namespace iTunesBlogger
{
	public class ConfigurationForm : System.Windows.Forms.Form
	{
		private iTunesBloggerConfig bloggerConfig;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.CheckBox cbTransparency;
		private System.Windows.Forms.CheckBox chkAlt;
		private System.Windows.Forms.CheckBox chkCtrl;
		private System.Windows.Forms.CheckBox chkEnableShortcut;
		private System.Windows.Forms.CheckBox chkEnableUpdating;
		private System.Windows.Forms.CheckBox chkShift;
		private System.Windows.Forms.ComboBox lbLetter;
		private System.Windows.Forms.GroupBox groupBox;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.GroupBox grpShortcutProperties;
		private System.Windows.Forms.GroupBox grpURLProperties;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.Label lblSpecifyShortcut;
		private System.Windows.Forms.Label lblUpdateType;
		private System.Windows.Forms.Label lblUpdateType2;
		private System.Windows.Forms.Label lblURLLegend;
		private System.Windows.Forms.ListBox lbUpdateType;
		private System.Windows.Forms.TextBox tbOpacityValue;
		private System.Windows.Forms.TextBox tbShortcutPattern;
		private System.Windows.Forms.TextBox tbURL;
		private System.Windows.Forms.TrackBar tbarOpacity;
		
		public iTunesBloggerConfig BloggerConfig {
			get {
				return bloggerConfig;
			}
		}
		
		public ConfigurationForm(iTunesBloggerConfig config)
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			chkAlt.Checked = config.alt;
			chkCtrl.Checked = config.ctrl;
			chkShift.Checked = config.shift;
			lbLetter.Text = config.letter;
			tbShortcutPattern.Text = config.trackText;
			tbURL.Text = config.url;
			tbarOpacity.Value = (int)config.opacityValue;
			tbOpacityValue.Text = ""+(tbarOpacity.Value*10)+"%";
			chkEnableShortcut.Checked = config.shortcutState;
			chkEnableUpdating.Checked = config.performUpdate;
			cbTransparency.Checked = config.isOpacityEnabled;

			switch (config.updateType) { 
				case "song": lbUpdateType.SelectedIndex = 0;
					break;
				case "album": lbUpdateType.SelectedIndex = 1;
					break;
				case "artist":lbUpdateType.SelectedIndex = 2;
					break;
			}
			
			if (cbTransparency.Checked){
				tbOpacityValue.Enabled = true;
				tbarOpacity.Enabled = true;
			}else {
				tbOpacityValue.Enabled = false;
				tbarOpacity.Enabled = false;
				MainForm.ActiveForm.Opacity = 1;
			}
			
			bloggerConfig = config;
		}
		
		#region Windows Forms Designer generated code
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent() {
			this.chkEnableShortcut = new System.Windows.Forms.CheckBox();
			this.lblUpdateType = new System.Windows.Forms.Label();
			this.tbURL = new System.Windows.Forms.TextBox();
			this.tbShortcutPattern = new System.Windows.Forms.TextBox();
			this.chkShift = new System.Windows.Forms.CheckBox();
			this.tbOpacityValue = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label8 = new System.Windows.Forms.Label();
			this.lbUpdateType = new System.Windows.Forms.ListBox();
			this.chkEnableUpdating = new System.Windows.Forms.CheckBox();
			this.lblURLLegend = new System.Windows.Forms.Label();
			this.tbarOpacity = new System.Windows.Forms.TrackBar();
			this.btnOK = new System.Windows.Forms.Button();
			this.lblSpecifyShortcut = new System.Windows.Forms.Label();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.chkAlt = new System.Windows.Forms.CheckBox();
			this.grpShortcutProperties = new System.Windows.Forms.GroupBox();
			this.btnCancel = new System.Windows.Forms.Button();
			this.lbLetter = new System.Windows.Forms.ComboBox();
			this.cbTransparency = new System.Windows.Forms.CheckBox();
			this.grpURLProperties = new System.Windows.Forms.GroupBox();
			this.lblUpdateType2 = new System.Windows.Forms.Label();
			this.chkCtrl = new System.Windows.Forms.CheckBox();
			this.groupBox = new System.Windows.Forms.GroupBox();
			((System.ComponentModel.ISupportInitialize)(this.tbarOpacity)).BeginInit();
			this.groupBox1.SuspendLayout();
			this.grpShortcutProperties.SuspendLayout();
			this.grpURLProperties.SuspendLayout();
			this.groupBox.SuspendLayout();
			this.SuspendLayout();
			// 
			// chkEnableShortcut
			// 
			this.chkEnableShortcut.Location = new System.Drawing.Point(8, 96);
			this.chkEnableShortcut.Name = "chkEnableShortcut";
			this.chkEnableShortcut.Size = new System.Drawing.Size(136, 16);
			this.chkEnableShortcut.TabIndex = 5;
			this.chkEnableShortcut.Text = "Enable Hot&key";
			this.chkEnableShortcut.CheckedChanged += new System.EventHandler(this.ShortcutCheckBoxCheckedChanged);
			// 
			// lblUpdateType
			// 
			this.lblUpdateType.Location = new System.Drawing.Point(8, 24);
			this.lblUpdateType.Name = "lblUpdateType";
			this.lblUpdateType.Size = new System.Drawing.Size(72, 32);
			this.lblUpdateType.TabIndex = 11;
			this.lblUpdateType.Text = "Update whenever the";
			// 
			// tbURL
			// 
			this.tbURL.Location = new System.Drawing.Point(8, 80);
			this.tbURL.Name = "tbURL";
			this.tbURL.Size = new System.Drawing.Size(176, 20);
			this.tbURL.TabIndex = 1;
			this.tbURL.Text = "";
			// 
			// tbShortcutPattern
			// 
			this.tbShortcutPattern.Location = new System.Drawing.Point(8, 72);
			this.tbShortcutPattern.Name = "tbShortcutPattern";
			this.tbShortcutPattern.Size = new System.Drawing.Size(176, 20);
			this.tbShortcutPattern.TabIndex = 6;
			this.tbShortcutPattern.Text = "";
			// 
			// chkShift
			// 
			this.chkShift.Location = new System.Drawing.Point(88, 32);
			this.chkShift.Name = "chkShift";
			this.chkShift.Size = new System.Drawing.Size(48, 24);
			this.chkShift.TabIndex = 3;
			this.chkShift.Text = "&shift";
			// 
			// tbOpacityValue
			// 
			this.tbOpacityValue.Location = new System.Drawing.Point(152, 16);
			this.tbOpacityValue.Name = "tbOpacityValue";
			this.tbOpacityValue.Size = new System.Drawing.Size(32, 20);
			this.tbOpacityValue.TabIndex = 0;
			this.tbOpacityValue.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 56);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(112, 16);
			this.label2.TabIndex = 7;
			this.label2.Text = "Text Pattern to Paste";
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(8, 64);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(96, 23);
			this.label8.TabIndex = 2;
			this.label8.Text = "Update URL";
			// 
			// lbUpdateType
			// 
			this.lbUpdateType.Items.AddRange(new object[] {
						"song",
						"album",
						"artist"});
			this.lbUpdateType.Location = new System.Drawing.Point(80, 16);
			this.lbUpdateType.Name = "lbUpdateType";
			this.lbUpdateType.Size = new System.Drawing.Size(48, 43);
			this.lbUpdateType.TabIndex = 10;
			// 
			// chkEnableUpdating
			// 
			this.chkEnableUpdating.Location = new System.Drawing.Point(8, 104);
			this.chkEnableUpdating.Name = "chkEnableUpdating";
			this.chkEnableUpdating.Size = new System.Drawing.Size(136, 24);
			this.chkEnableUpdating.TabIndex = 4;
			this.chkEnableUpdating.Text = "Enable Web &Update";
			this.chkEnableUpdating.CheckedChanged += new System.EventHandler(this.CheckBoxCheckedChanged);
			// 
			// lblURLLegend
			// 
			this.lblURLLegend.Font = new System.Drawing.Font("Tahoma", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World);
			this.lblURLLegend.Location = new System.Drawing.Point(16, 16);
			this.lblURLLegend.Name = "lblURLLegend";
			this.lblURLLegend.Size = new System.Drawing.Size(160, 72);
			this.lblURLLegend.TabIndex = 4;
			this.lblURLLegend.Text = "Wildcards: %a - artist, %t - track title, %b - album title, %g - genre, %y - trac" +
"k time, %d - track year, %n - track number, %p - disc number, %c - comment, %k -" +
" kind, %r - rating";
			// 
			// tbarOpacity
			// 
			this.tbarOpacity.LargeChange = 1;
			this.tbarOpacity.Location = new System.Drawing.Point(8, 40);
			this.tbarOpacity.Name = "tbarOpacity";
			this.tbarOpacity.Size = new System.Drawing.Size(168, 34);
			this.tbarOpacity.TabIndex = 4;
			this.tbarOpacity.Scroll += new System.EventHandler(this.TbarOpacityScroll);
			// 
			// btnOK
			// 
			this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.btnOK.Location = new System.Drawing.Point(248, 248);
			this.btnOK.Name = "btnOK";
			this.btnOK.TabIndex = 0;
			this.btnOK.Text = "&OK";
			this.btnOK.Click += new System.EventHandler(this.BtnOKClick);
			// 
			// lblSpecifyShortcut
			// 
			this.lblSpecifyShortcut.Location = new System.Drawing.Point(8, 16);
			this.lblSpecifyShortcut.Name = "lblSpecifyShortcut";
			this.lblSpecifyShortcut.Size = new System.Drawing.Size(176, 16);
			this.lblSpecifyShortcut.TabIndex = 1;
			this.lblSpecifyShortcut.Text = "Specify Hotkey Combination";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.lblURLLegend);
			this.groupBox1.Location = new System.Drawing.Point(208, 144);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(192, 96);
			this.groupBox1.TabIndex = 24;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Wildcards";
			// 
			// chkAlt
			// 
			this.chkAlt.Location = new System.Drawing.Point(8, 32);
			this.chkAlt.Name = "chkAlt";
			this.chkAlt.Size = new System.Drawing.Size(40, 24);
			this.chkAlt.TabIndex = 0;
			this.chkAlt.Text = "&alt";
			// 
			// grpShortcutProperties
			// 
			this.grpShortcutProperties.Controls.Add(this.label2);
			this.grpShortcutProperties.Controls.Add(this.tbShortcutPattern);
			this.grpShortcutProperties.Controls.Add(this.chkEnableShortcut);
			this.grpShortcutProperties.Controls.Add(this.lbLetter);
			this.grpShortcutProperties.Controls.Add(this.chkShift);
			this.grpShortcutProperties.Controls.Add(this.chkCtrl);
			this.grpShortcutProperties.Controls.Add(this.lblSpecifyShortcut);
			this.grpShortcutProperties.Controls.Add(this.chkAlt);
			this.grpShortcutProperties.Location = new System.Drawing.Point(8, 0);
			this.grpShortcutProperties.Name = "grpShortcutProperties";
			this.grpShortcutProperties.Size = new System.Drawing.Size(192, 120);
			this.grpShortcutProperties.TabIndex = 22;
			this.grpShortcutProperties.TabStop = false;
			this.grpShortcutProperties.Text = "Hotkey Settings";
			// 
			// btnCancel
			// 
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Location = new System.Drawing.Point(328, 248);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.TabIndex = 1;
			this.btnCancel.Text = "&Cancel";
			// 
			// lbLetter
			// 
			this.lbLetter.AllowDrop = true;
			this.lbLetter.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.lbLetter.Items.AddRange(new object[] {
						"0",
						"1",
						"2",
						"3",
						"4",
						"5",
						"6",
						"7",
						"8",
						"9",
						"A",
						"B",
						"C",
						"D",
						"E",
						"F",
						"G",
						"H",
						"I",
						"J",
						"K",
						"L",
						"M",
						"N",
						"O",
						"P",
						"Q",
						"R",
						"S",
						"T",
						"U",
						"V",
						"W",
						"X",
						"Y",
						"Z"});
			this.lbLetter.Location = new System.Drawing.Point(136, 32);
			this.lbLetter.Name = "lbLetter";
			this.lbLetter.Size = new System.Drawing.Size(48, 21);
			this.lbLetter.Sorted = true;
			this.lbLetter.TabIndex = 4;
			// 
			// cbTransparency
			// 
			this.cbTransparency.Location = new System.Drawing.Point(16, 16);
			this.cbTransparency.Name = "cbTransparency";
			this.cbTransparency.TabIndex = 3;
			this.cbTransparency.Text = "&Transparency";
			this.cbTransparency.CheckedChanged += new System.EventHandler(this.Cb_transparencyCheckedChanged);
			// 
			// grpURLProperties
			// 
			this.grpURLProperties.Controls.Add(this.lblUpdateType2);
			this.grpURLProperties.Controls.Add(this.lblUpdateType);
			this.grpURLProperties.Controls.Add(this.lbUpdateType);
			this.grpURLProperties.Controls.Add(this.chkEnableUpdating);
			this.grpURLProperties.Controls.Add(this.tbURL);
			this.grpURLProperties.Controls.Add(this.label8);
			this.grpURLProperties.Location = new System.Drawing.Point(208, 0);
			this.grpURLProperties.Name = "grpURLProperties";
			this.grpURLProperties.Size = new System.Drawing.Size(192, 136);
			this.grpURLProperties.TabIndex = 21;
			this.grpURLProperties.TabStop = false;
			this.grpURLProperties.Text = "Web Update Settings";
			// 
			// lblUpdateType2
			// 
			this.lblUpdateType2.Location = new System.Drawing.Point(136, 32);
			this.lblUpdateType2.Name = "lblUpdateType2";
			this.lblUpdateType2.Size = new System.Drawing.Size(48, 23);
			this.lblUpdateType2.TabIndex = 12;
			this.lblUpdateType2.Text = "changes.";
			// 
			// chkCtrl
			// 
			this.chkCtrl.Location = new System.Drawing.Point(48, 32);
			this.chkCtrl.Name = "chkCtrl";
			this.chkCtrl.Size = new System.Drawing.Size(40, 24);
			this.chkCtrl.TabIndex = 2;
			this.chkCtrl.Text = "&ctrl";
			// 
			// groupBox
			// 
			this.groupBox.Controls.Add(this.tbarOpacity);
			this.groupBox.Controls.Add(this.cbTransparency);
			this.groupBox.Controls.Add(this.tbOpacityValue);
			this.groupBox.Location = new System.Drawing.Point(8, 120);
			this.groupBox.Name = "groupBox";
			this.groupBox.Size = new System.Drawing.Size(192, 88);
			this.groupBox.TabIndex = 23;
			this.groupBox.TabStop = false;
			this.groupBox.Text = "Opacity";
			// 
			// ConfigurationForm
			// 
			this.AcceptButton = this.btnOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.btnCancel;
			this.ClientSize = new System.Drawing.Size(410, 277);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.groupBox);
			this.Controls.Add(this.grpShortcutProperties);
			this.Controls.Add(this.grpURLProperties);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnOK);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "ConfigurationForm";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "iTunesBlogger Settings";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.ConfigurationFormClosing);
			((System.ComponentModel.ISupportInitialize)(this.tbarOpacity)).EndInit();
			this.groupBox1.ResumeLayout(false);
			this.grpShortcutProperties.ResumeLayout(false);
			this.grpURLProperties.ResumeLayout(false);
			this.groupBox.ResumeLayout(false);
			this.ResumeLayout(false);
		}
		#endregion
		void ConfigurationFormClosing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			bloggerConfig.alt = chkAlt.Checked;
			bloggerConfig.ctrl  = chkCtrl.Checked;
			bloggerConfig.isOpacityEnabled = cbTransparency.Checked;
			bloggerConfig.letter = lbLetter.Text;
			bloggerConfig.opacityValue = tbarOpacity.Value; 
			bloggerConfig.performUpdate = chkEnableUpdating.Checked;
			bloggerConfig.shift = chkShift.Checked;
			bloggerConfig.shortcutState = chkEnableShortcut.Checked;
			bloggerConfig.trackText = tbShortcutPattern.Text;
			bloggerConfig.updateType = lbUpdateType.SelectedItem.ToString();
			bloggerConfig.url = tbURL.Text;
		}
		
		// handler for the "enable web updating" checkbox		
		void CheckBoxCheckedChanged(object sender, System.EventArgs e)
		{
			if (tbURL.Text !="") {
				if (chkEnableUpdating.Checked){
					tbURL.ReadOnly = true;
					tbURL.Enabled = false;
				} else {	
					tbURL.ReadOnly = false;
					tbURL.Enabled = true;
				}
			} else {
				if (chkEnableUpdating.Checked) 
					MessageBox.Show("A valid URL is needed", "Warning", 
					                System.Windows.Forms.MessageBoxButtons.OK, 
					                System.Windows.Forms.MessageBoxIcon.Error);
				chkEnableUpdating.Checked = false;
			}
		}
		
		// handler for the "enable keyboard shortcut" checkbox
		void ShortcutCheckBoxCheckedChanged(object sender, System.EventArgs e)
		{
			if (chkEnableShortcut.Checked){
				chkAlt.Enabled = false;
				chkCtrl.Enabled = false;
				chkShift.Enabled = false;
				lbLetter.Enabled = false;
				tbShortcutPattern.Enabled = false;
			} else {
				chkAlt.Enabled = true;
				chkCtrl.Enabled = true;
				chkShift.Enabled = true;
				lbLetter.Enabled = true;
				tbShortcutPattern.Enabled = true;
			}
		}
		
		void Cb_transparencyCheckedChanged(object sender, System.EventArgs e)
		{
			if (cbTransparency.Checked){
				tbOpacityValue.Enabled = true;
				tbarOpacity.Enabled = true;
				
			} else {
				tbOpacityValue.Enabled = false;
				tbarOpacity.Enabled = false;
				MainForm.ActiveForm.Opacity = 1;
			}
		}
		
		void TbarOpacityScroll(object sender, System.EventArgs e)
		{
			tbOpacityValue.Text = ""+(tbarOpacity.Value*10) +"%";
		}
		
		void BtnOKClick(object sender, System.EventArgs e)
		{
			
		}
	}
}
